package com.zzh.lib.core.utils;


import android.text.TextUtils;
import android.text.style.StyleSpan;

import com.zzh.lib.core.lang.HStrUtils;
import com.zzh.lib.core.lang.HStringSpannable;
import com.zzh.lib.core.lang.span.TextShapeSpan;

/**
 * Created by zzh on 2024/11/13.
 *
 * @date: 2024/11/13 16:58
 * @email: zzh_hz@126.com
 * @author: zzh
 * @description: com.zzh.lib.core.utils
 */
public class HTagUtils {
    public static void setTagFill(HStringSpannable hss, String tag, int textSize,
                                  float padding,
                                  float margin,
                                  int bgColor, int textColor, int radius) {
        appendTagFill(hss, false, tag, textSize, padding, padding, padding, padding,
                margin, margin, margin, margin, bgColor, textColor, radius);
    }

    public static void setTagStroke(HStringSpannable hss, String tag, int textSize,
                                    float padding,
                                    float margin,
                                    int bgColor, int textColor, int radius, float strokeWidth, int strokeColor) {
        appendTagStroke(hss, false, tag, textSize, padding, padding, padding, padding,
                margin, margin, margin, margin,
                bgColor, textColor, radius, strokeWidth, strokeColor);
    }


    public static void setTagFill(HStringSpannable hss, String tag, int textSize, float paddingStart, float paddingTop, float paddingEnd, float paddingBottom,
                                  float marginStart, float marginTop, float marginEnd, float marginBottom, int bgColor, int textColor, int radius) {
        appendTagFill(hss, false, tag, textSize, paddingStart, paddingTop, paddingEnd, paddingBottom,
                marginStart, marginTop, marginEnd, marginBottom, bgColor, textColor, radius);
    }

    public static void setTagStroke(HStringSpannable hss, String tag, int textSize,
                                    float paddingStart, float paddingTop, float paddingEnd, float paddingBottom,
                                    float marginStart, float marginTop, float marginEnd, float marginBottom,
                                    int bgColor, int textColor, int radius, float strokeWidth, int strokeColor) {
        appendTagStroke(hss, false, tag, textSize, paddingStart, paddingTop, paddingEnd, paddingBottom,
                marginStart, marginTop, marginEnd, marginBottom,
                bgColor, textColor, radius, strokeWidth, strokeColor);
    }

    public static void appendTagFill(HStringSpannable hss, boolean append, String tag, int textSize,
                                     float padding,
                                     float margin, int bgColor, int textColor, int radius) {
        if (hss == null || TextUtils.isEmpty(tag)) {
            return;
        }
        if (append) {
            hss.append(tag);
        }
        int[] position = HStrUtils.getStringFirstPosition(hss.toString(), tag);
        if (position[0] < 0) {
            return;
        }
        TextShapeSpan tss = new TextShapeSpan();
        tss.setStyle(TextShapeSpan.STYLE_FILL);
        tss.setTextSize(textSize);
        tss.setPadding(padding, padding, padding, padding).setMargin(margin, margin, margin, margin);
        tss.setBackgroundColor(bgColor).setTxtColor(textColor).setRadius(radius);
        hss.setSpan(tss, position[0], position[1]);
    }

    public static void appendTagStroke(HStringSpannable hss, boolean append, String tag, int textSize,
                                       float padding,
                                       float margin,
                                       int bgColor, int textColor, int radius, float strokeWidth, int strokeColor) {
        if (hss == null || TextUtils.isEmpty(tag)) {
            return;
        }
        if (append) {
            hss.append(tag);
        }
        int[] position = HStrUtils.getStringFirstPosition(hss.toString(), tag);
        if (position[0] < 0) {
            return;
        }
        TextShapeSpan tss = new TextShapeSpan();
        tss.setStyle(TextShapeSpan.STYLE_STROKE);
        tss.setTextSize(textSize);
        tss.setPadding(padding, padding, padding, padding)
                .setMargin(margin, margin, margin, margin);
        tss.setBackgroundColor(bgColor).setTxtColor(textColor).setRadius(radius);
        tss.setStrokeColor(strokeColor);
        tss.setStrokeWidth(strokeWidth);
        hss.setSpan(tss, position[0], position[1]);
    }

    public static void appendTagFill(HStringSpannable hss, boolean append, String tag, int textSize, float paddingStart, float paddingTop, float paddingEnd, float paddingBottom,
                                     float marginStart, float marginTop, float marginEnd, float marginBottom, int bgColor, int textColor, int radius) {
        if (hss == null || TextUtils.isEmpty(tag)) {
            return;
        }
        if (append) {
            hss.append(tag);
        }
        int[] position = HStrUtils.getStringFirstPosition(hss.toString(), tag);
        if (position[0] < 0) {
            return;
        }
        TextShapeSpan tss = new TextShapeSpan();
        tss.setStyle(TextShapeSpan.STYLE_FILL);
        tss.setTextSize(textSize);
        tss.setPadding(paddingStart, paddingTop, paddingEnd, paddingBottom).setMargin(marginStart, marginTop, marginEnd, marginBottom);
        tss.setBackgroundColor(bgColor).setTxtColor(textColor).setRadius(radius);
        hss.setSpan(tss, position[0], position[1]);
    }

    public static void appendTagStroke(HStringSpannable hss, boolean append, String tag, int textSize,
                                       float paddingStart, float paddingTop, float paddingEnd, float paddingBottom,
                                       float marginStart, float marginTop, float marginEnd, float marginBottom,
                                       int bgColor, int textColor, int radius, float strokeWidth, int strokeColor) {
        if (hss == null || TextUtils.isEmpty(tag)) {
            return;
        }
        if (append) {
            hss.append(tag);
        }
        int[] position = HStrUtils.getStringFirstPosition(hss.toString(), tag);
        if (position[0] < 0) {
            return;
        }
        TextShapeSpan tss = new TextShapeSpan();
        tss.setStyle(TextShapeSpan.STYLE_STROKE);
        tss.setTextSize(textSize);
        tss.setPadding(paddingStart, paddingTop, paddingEnd, paddingBottom)
                .setMargin(marginStart, marginTop, marginEnd, marginBottom);
        tss.setBackgroundColor(bgColor).setTxtColor(textColor).setRadius(radius);
        tss.setStrokeColor(strokeColor);
        tss.setStrokeWidth(strokeWidth);
        hss.setSpan(tss, position[0], position[1]);
    }

    public static void setTagDrawable(HStringSpannable hss, String tag, int textSize, float padding,
                                      float margin, int bgRes, int textColor) {
        appendTagDrawable(hss, false, tag, textSize,
                padding, padding, padding, padding,
                margin, margin, margin, margin, bgRes, textColor);
    }

    public static void appendTagDrawable(HStringSpannable hss, boolean append, String tag, int textSize, float padding,
                                         float margin, int bgRes, int textColor) {
        appendTagDrawable(hss, append, tag, textSize,
                padding, padding, padding, padding,
                margin, margin, margin, margin, bgRes, textColor);
    }

    public static void appendTagDrawable(HStringSpannable hss, boolean append, String tag, int textSize, float paddingStart, float paddingTop, float paddingEnd, float paddingBottom,
                                         float marginStart, float marginTop, float marginEnd, float marginBottom, int bgRes, int textColor) {
        if (hss == null || TextUtils.isEmpty(tag)) {
            return;
        }
        if (append) {
            hss.append(tag);
        }
        int[] position = HStrUtils.getStringFirstPosition(hss.toString(), tag);
        if (position[0] < 0) {
            return;
        }
        TextShapeSpan tss = new TextShapeSpan();
        tss.setStyle(TextShapeSpan.STYLE_FILL);
        tss.setTextSize(textSize);
        tss.setPadding(paddingStart, paddingTop, paddingEnd, paddingBottom).setMargin(marginStart, marginTop, marginEnd, marginBottom);
        tss.setBackgroundResource(bgRes).setTxtColor(textColor);
        hss.setSpan(tss, position[0], position[1]);
    }
}
